﻿using Me.Amon.Da.Db;
using Me.Amon.Da.Db.OMS;
using Me.Amon.Da.Db.POS;

namespace Me.Amon.Util
{
    public class KeyUtil
    {
        public static string NextSpuKey(RDBEngine dba, string corp)
        {
            int key = 0;

            dba.AddTable(DSpu.TABLE);
            dba.AddColumn(string.Format("MAX({0})", DSpu.CODE), DSpu.CODE);
            dba.AddWhere(DSpu.CORP_ID, corp);

            var dt = dba.ExecuteSelect();
            if (dt.Rows.Count > 0)
            {
                var tmp = "" + dt.Rows[0][0];
                if (!string.IsNullOrWhiteSpace(tmp))
                {
                    key = int.Parse(tmp.Substring(2));
                }
            }

            key += 1;
            return "SP" + key.ToString("000000");
        }

        public static string NextEmpKey(RDBEngine dba, string corp)
        {
            int key = 0;

            dba.AddTable(DEmp.TABLE);
            dba.AddColumn(string.Format("MAX({0})", DEmp.CODE), DEmp.CODE);
            dba.AddWhere(DEmp.CORP_ID, corp);

            var dt = dba.ExecuteSelect();
            if (dt.Rows.Count < 1)
            {
                key = 0;
            }
            else
            {
                var tmp = "" + dt.Rows[0][0];
                if (!string.IsNullOrWhiteSpace(tmp))
                {
                    key = int.Parse(tmp.Substring(2));
                }
            }

            key += 1;
            return "YG" + key.ToString("000000");
        }

        public static string NextVipKey(RDBEngine dba, string corp)
        {
            int key = 0;

            dba.AddTable(DVip.TABLE);
            dba.AddColumn(string.Format("MAX({0})", DVip.CODE), DVip.CODE);
            dba.AddWhere(DVip.CORP_ID, corp);

            var dt = dba.ExecuteSelect();
            if (dt.Rows.Count < 1)
            {
                key = 0;
            }
            else
            {
                var tmp = "" + dt.Rows[0][0];
                if (!string.IsNullOrWhiteSpace(tmp))
                {
                    key = int.Parse(tmp.Substring(2));
                }
            }

            key += 1;
            return "HY" + key.ToString("000000");
        }

        public static string NextShopKey(RDBEngine dba, string corp)
        {
            int key = 0;

            dba.AddTable(DShop.TABLE);
            dba.AddColumn(string.Format("MAX({0})", DShop.CODE), DShop.CODE);
            dba.AddWhere(DShop.CORP_ID, corp);

            var dt = dba.ExecuteSelect();
            if (dt.Rows.Count < 1)
            {
                key = 0;
            }
            else
            {
                var tmp = "" + dt.Rows[0][0];
                if (!string.IsNullOrWhiteSpace(tmp))
                {
                    key = int.Parse(tmp.Substring(2));
                }
            }

            key += 1;
            return "MD" + key.ToString("000000");
        }
        public static string NextTerminalKey(RDBEngine dba, string corp)
        {
            int key = 0;

            dba.AddTable(DTerminal.TABLE);
            dba.AddColumn(string.Format("MAX({0})", DTerminal.CODE), DTerminal.CODE);
            dba.AddWhere(DTerminal.CORP_ID, corp);

            var dt = dba.ExecuteSelect();
            if (dt.Rows.Count < 1)
            {
                key = 0;
            }
            else
            {
                var tmp = "" + dt.Rows[0][0];
                if (!string.IsNullOrWhiteSpace(tmp))
                {
                    key = int.Parse(tmp.Substring(2));
                }
            }

            key += 1;
            return "ZD" + key.ToString("000000");
        }
    }
}